using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BusinessLayer;
using EntityLayer;
using WebControls;

public partial class Admin_PhotoAlbums : SFPage
{
    busPhotoAlbums insBusPhotoAlbums = new busPhotoAlbums();
    entPhotoAlbums insEntPhotoAlbums = new entPhotoAlbums();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected bool sfgPhotoAlbums_NeedsDataSource()
    {
        DataTable insDataTablePhotoAlbums = insBusPhotoAlbums.SelectPhotoAlbums();
        if (insBusPhotoAlbums.HasErrors)
        {
            MessageBoxShow(insBusPhotoAlbums);
        }
        sfgPhotoAlbums.SetData(insDataTablePhotoAlbums);
        return insBusPhotoAlbums.HasErrors;
    }
    protected void btnNew_Click(object sender, EventArgs e)
    {
        this.Id = 0;
        this.OpenEditPanel();

    }
    protected void sfgPhotoAlbums_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "EditData")
        {
            this.Id = Convert.ToInt32(e.CommandArgument);
            this.OpenEditPanel();
        }
        else if (e.CommandName == "DeleteData")
        {
            insEntPhotoAlbums.PhotoAlbumId = Convert.ToInt32(e.CommandArgument);
            insBusPhotoAlbums.DeletePhotoAlbumsByPhotoAlbumId(insEntPhotoAlbums);
            if (insBusPhotoAlbums.HasErrors)
            {
                MessageBoxShow(insBusPhotoAlbums);
                return;
            }
            sfgPhotoAlbums.LoadGridData();
            this.CloseEditPanel();
        }
    }
    public override void OpenEditPanel()
    {
        this.Clear(this.Master.FindControl("ContentPlaceHolderEdit"));
        base.OpenEditPanel();
        lstPhotoAlbums.LoadDataListData();

    }
    protected bool lstPhotoAlbums_NeedsDataSource()
    {
        insEntPhotoAlbums.PhotoAlbumId = this.Id;
        insBusPhotoAlbums.SelectPhotoAlbumsByPhotoAlbumId(insEntPhotoAlbums);
        if (insBusPhotoAlbums.HasErrors)
        {
            MessageBoxShow(insBusPhotoAlbums);
            return true;
        }
        txtPhotoAlbumDescription.Text = insEntPhotoAlbums.PhotoAlbumDescription;


        entPhotoAlbumContents insEntPhotoAlbumContents = new entPhotoAlbumContents();
        insEntPhotoAlbumContents.PhotoAlbumId = insEntPhotoAlbums.PhotoAlbumId;
        busPhotoAlbumContents insBusPhotoAlbumContents = new busPhotoAlbumContents();
        DataTable insDataTablePhotoAlbumContents = insBusPhotoAlbumContents.SelectPhotoAlbumContentsByPhotoAlbumId(insEntPhotoAlbumContents);
        if (insBusPhotoAlbumContents.HasErrors)
        {
            MessageBoxShow(insBusPhotoAlbumContents);
            return true;
        }
        lstPhotoAlbums.SetData(insDataTablePhotoAlbumContents);
        return false;
    }

    public override void Save()
    {

        this.Validate();
        if (this.HasErrors)
        {
            MessageBoxShow(this);
            return;
        }

        insEntPhotoAlbums.PhotoAlbumDescription = txtPhotoAlbumDescription.Text;
        insEntPhotoAlbums.PublishDate = calPublishDate.Value.Value;

        foreach (DataListItem dli in lstPhotoAlbums.Items)
        {
            entPhotoAlbumContents insEntPhotoAlbumContents = new entPhotoAlbumContents();
            if (((CheckBox)dli.FindControl("chkLanguage")).Checked)
            {
                insEntPhotoAlbumContents.LanguageId = Convert.ToInt32(((SFCheckBox)dli.FindControl("chkLanguage")).Tag);
                insEntPhotoAlbumContents.PhotoAlbumName = ((SFTextBox)dli.FindControl("txtPhotoAlbumName")).Text;
                insEntPhotoAlbumContents.PhotoAlbumContentDescription = ((SFTextBox)dli.FindControl("txtPhotoAlbumDescription")).Text;
                if (lstPhotoAlbums.DataKeys[dli.ItemIndex] != DBNull.Value)
                {
                    insEntPhotoAlbumContents.PhotoAlbumContentId = Convert.ToInt32(lstPhotoAlbums.DataKeys[dli.ItemIndex]);
                    insEntPhotoAlbums.PhotoAlbumContentsUpdate.Add(insEntPhotoAlbumContents);
                }
                else
                {
                    insEntPhotoAlbums.PhotoAlbumContentsInsert.Add(insEntPhotoAlbumContents);
                }
            }
            else
            {
                if (lstPhotoAlbums.DataKeys[dli.ItemIndex] != DBNull.Value)
                {
                    insEntPhotoAlbumContents.PhotoAlbumContentId = Convert.ToInt32(lstPhotoAlbums.DataKeys[dli.ItemIndex]);
                    insEntPhotoAlbums.PhotoAlbumContentsDelete.Add(insEntPhotoAlbumContents);
                }
            }
        }
        base.Save();
    }
    public override void SaveNew()
    {
        insBusPhotoAlbums.InsertPhotoAlbums(insEntPhotoAlbums);
        if (insBusPhotoAlbums.HasErrors)
        {
            MessageBoxShow(insBusPhotoAlbums);
            return;
        }
        base.SaveNew();
        sfgPhotoAlbums.LoadGridData();
    }
    public override void SaveExisting()
    {
        insEntPhotoAlbums.PhotoAlbumId = this.Id;
        insBusPhotoAlbums.UpdatePhotoAlbumsByPhotoAlbumId(insEntPhotoAlbums);
        if (insBusPhotoAlbums.HasErrors)
        {
            MessageBoxShow(insBusPhotoAlbums);
            return;
        }
        base.SaveExisting();
        sfgPhotoAlbums.LoadGridData();
    }

    protected void lstPhotoAlbums_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item != null && e.Item.FindControl("chkLanguage") != null && e.Item.DataItem != null)
        {
            if (this.Id != 0)
            {
                ((SFCheckBox)e.Item.FindControl("chkLanguage")).Checked = (((DataRowView)e.Item.DataItem)["PhotoAlbumContentId"] != DBNull.Value);
                e.Item.FindControl("pnlPhotoAlbumContents").Visible = ((SFCheckBox)e.Item.FindControl("chkLanguage")).Checked;
            }
            else
            {
                ((SFCheckBox)e.Item.FindControl("chkLanguage")).Checked = true;
            }
            ((SFCheckBox)e.Item.FindControl("chkLanguage")).Tag = ((DataRowView)e.Item.DataItem)["LanguageId"].ToString();
        }
    }
    protected void Check(object sender, System.EventArgs e)
    {
        this.OpenEditPanelBase();
        ((SFCheckBox)sender).NamingContainer.FindControl("pnlPhotoAlbumContents").Visible = ((SFCheckBox)sender).Checked;
    }
}

